package com.bawei.image

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.bawei.image.loader.FrescoImageLoader
import com.bawei.image.loader.GlideImgLoader
import com.bawei.image.loader.NativeImageLoader
import com.bawei.image.loader.PicassoImageLoader
import com.bawei.image.option.ImageOptions
import com.bawei.image.skill.IImageLoader
import com.bawei.image.strategy.ImageStrategy
import java.io.File

/**
 * Author: Jason Zhang
 * Date: 2024/4/23
 * Desc: 支持多种加载策略的图片加载器
 * 备注1：ImgLoader.Builder().setImageStrategy(ImageStrategy.GLIDE).build().loadFileIntoImageView(...)
 * 备注2：ImgLoader.Builder().build().loadFileIntoImageView(...)
 */
class ImgLoader private constructor(private var strategy: ImageStrategy = ImageStrategy.GLIDE):  IImageLoader{

    private var imgLoaderStrategy = when(strategy){
        ImageStrategy.GLIDE -> GlideImgLoader()
        ImageStrategy.FRESCO -> FrescoImageLoader()
        ImageStrategy.PICASSO -> PicassoImageLoader()
        ImageStrategy.NATIVE -> NativeImageLoader()
    }

    companion object Builder{

        private var strategy: ImageStrategy = ImageStrategy.GLIDE

        fun setImageStrategy(strategy: ImageStrategy){
            this.strategy = strategy
        }

        fun build(): ImgLoader = ImgLoader(strategy)

    }

    override fun loadFileIntoImageView(
        context: Context,
        file: File,
        target: ImageView,
        options: ImageOptions
    ) {
       //拿到具体的图片加载器，来加载图片
        imgLoaderStrategy.loadFileIntoImageView(context, file, target, options)
    }

    override fun loadResourceIntoImageView(
        context: Context,
        resId: Int,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadResourceIntoImageView(context, resId, target, options)
    }

    override fun loadUriIntoImageView(
        context: Context,
        uri: Uri,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadUriIntoImageView(context, uri, target, options)
    }

    override fun loadUrlIntoImageView(
        context: Context,
        url: String,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadUrlIntoImageView(context, url, target, options)
    }

    override fun loadByteArrayIntoImageView(
        context: Context,
        bytes: ByteArray,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadByteArrayIntoImageView(context, bytes, target, options)

    }




}